package com.sign2024.sign2024core.common.utils;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.internal.OSSUtils;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;

import java.io.IOException;
import java.io.InputStream;

/**
 * oss工具类
 */
public class OssUtils {

    /**
     * endpoint 内网地址
     */
    public static final String ENDPOINT_INTERNAL = "";
    /**
     * endpoint 公网地址
     */
    public static final String ENDPOINT = "oss-cn-beijing.aliyuncs.com";
    public static final String ACCESS_KEY_ID = "LTAI5tKFPzYcWg2gGuyXYGBm";
    public static final String ACCESS_KEY_SECRET = "DdfXyqEiBRjcd1nYTtnrFp7guIxCk4";
    public static final String BUCKET_NAME = "yl-aly";
    //文件存储目录
    private static final String fileDir = "img/";

    private static OSS ossClient;

    /**
     * 单例
     *
     * @return OSS工具类实例
     */
    private static OSS getOSSClient() {
        if (ossClient == null) {
            synchronized (OSSUtils.class) {
                if (ossClient == null) {
                    ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
                }
            }
        }
        return ossClient;
    }

    /**
     * 上传到OSS服务器  如果同名文件会覆盖服务器上的
     *
     * @param inputStream 文件流
     * @param fileName    文件名称 包括后缀名
     * @return 返回路径
     */
    public static String uploadFile2OSS(InputStream inputStream, String fileName) {
        String path = "";
        try {
            //创建上传Object的Metadata
            ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setContentLength(inputStream.available());
            objectMetadata.setCacheControl("no-cache");
            objectMetadata.setHeader("Pragma", "no-cache");
            objectMetadata.setContentType(getContentType(fileName.substring(fileName.lastIndexOf("."))));
            objectMetadata.setContentDisposition("inline;filename=" + fileName);
            //上传文件
            PutObjectResult putResult = OssUtils.getOSSClient().putObject(BUCKET_NAME, fileDir + fileName, inputStream, objectMetadata);
            path = String.format("http://%s.%s/%s%s", BUCKET_NAME, ENDPOINT, fileDir, fileName);
        } catch (IOException e) {
            // log.error(e.getMessage(), e);
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return path;
    }

    /**
     * Description: 判断OSS服务文件上传时文件的contentType
     *
     * @param FilenameExtension 文件后缀
     * @return String
     */
    private static String getContentType(String FilenameExtension) {
        if (FilenameExtension.equalsIgnoreCase(".bmp")) {
            return "image/bmp";
        }
        if (FilenameExtension.equalsIgnoreCase(".gif")) {
            return "image/gif";
        }
        if (FilenameExtension.equalsIgnoreCase(".jpeg") ||
                FilenameExtension.equalsIgnoreCase(".jpg") ||
                FilenameExtension.equalsIgnoreCase(".png")) {
            return "image/jpeg";
        }
        if (FilenameExtension.equalsIgnoreCase(".html")) {
            return "text/html";
        }
        if (FilenameExtension.equalsIgnoreCase(".txt")) {
            return "text/plain";
        }
        if (FilenameExtension.equalsIgnoreCase(".vsd")) {
            return "application/vnd.visio";
        }
        if (FilenameExtension.equalsIgnoreCase(".pptx") ||
                FilenameExtension.equalsIgnoreCase(".ppt")) {
            return "application/vnd.ms-powerpoint";
        }
        if (FilenameExtension.equalsIgnoreCase(".docx") ||
                FilenameExtension.equalsIgnoreCase(".doc")) {
            return "application/msword";
        }
        if (FilenameExtension.equalsIgnoreCase(".xml")) {
            return "text/xml";
        }
        return "image/jpeg";
    }

}
